package com.hanhan.chargingpile.user.controller.api;


import com.hanhan.chargingpile.user.pojo.vo.AppUserVO;
import com.hanhan.chargingpile.user.pojo.vo.LoginVO;
import com.hanhan.chargingpile.user.service.AppUserService;
import com.hanhan.common.exception.Assert;
import com.hanhan.common.result.R;
import com.hanhan.common.result.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author HanHan
 * @since 2023-03-23
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user/appUser")
public class AppUserController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private AppUserService appUserService;

    @ApiOperation(value = "用户登录(未注册用户会自动注册)")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String tel = loginVO.getTel();
        String code = loginVO.getCode();
        String codeGen = (String) redisTemplate.opsForValue().get("hanhan:sms:code:" + tel);
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);

        String ip = request.getRemoteAddr();
        AppUserVO appUserVO = appUserService.login(loginVO, ip);

        return R.ok().data("appUserVO", appUserVO);
    }

    @ApiOperation(value = "检验手机号")
    @GetMapping("/checkTel/{tel}")
    public R checkTel(
            @ApiParam(value = "手机号", required = true)
            @PathVariable String tel){
        return null;
    }
}

